from django.shortcuts import render, redirect,reverse
from django import http
from django.views import View
import json


# Create your views here.
# #
# def register(request):
#     if request.method == 'GET':
#         return http.HttpResponse('这是个注册页面')
#     else:
#         return http.HttpResponse('这是个很屌的注册页面')
class IndexView(View):
    def get(self, request):
        return http.HttpResponse('假装这是个首页')


class LoginRedirectView(View):
    def post(self, request):
        # return redirect('/index/')
        return redirect(reverse('users:index'))


class RedirectView(View):
    def get(self, request):
        # return redirect('/users/login/')
        return redirect(reverse('users:login'))


class ResponseJsonView(View):
    def get(self, request):
        # dict_data = {
        #     'city': 'beijing',
        #     'subject': 'python'
        # }
        list_data = [{
            'city': 'beijing',
            'subject': 'python'
        }, {
            'city': '子线程',
            'subject': '123'
        }]
        return http.JsonResponse(list_data, safe=False)


class QueryString(View):
    def get(self, request):
        str_param = request.GET
        name = str_param.get('name')
        age = str_param.get('age')
        email = str_param.get('email')
        print(name, age, email)
        return http.HttpResponse('字符串传参')


class FromDataView(View):
    def post(self, request):
        str_param = request.POST
        name = str_param.get('name')
        age = str_param.get('age')
        email = str_param.get('email')
        print(name, age, email)
        return http.HttpResponse('表单传参')


class JsonView(View):
    def post(self, request):
        json_dict = json.loads(request.body)
        name = json_dict.get('name')
        age = json_dict.get('age')
        email = json_dict.get('email')
        print(name, age, email)
        return http.HttpResponse('JSON字符串传参')


class PathView(View):
    def get(self, request, name, age, Email):
        print(name, age, Email)
        return http.HttpResponse(f'name:{name}\nage:{age}\nemail:{Email}')


class UrlParam3View(View):
    def get(self, request, mobile_num):
        return http.HttpResponse(f'mobile_num:{mobile_num}')


class UrlParam2View(View):
    def get(self, request, phone_num):
        return http.HttpResponse(f'age={phone_num}')


class JsonParamView(View):
    def post(self, request):
        json_dict = json.loads(request.body)
        username = json_dict.get('username')
        password = json_dict.get('password')
        return http.HttpResponse(f'username:{username}\npassword{password}')


# 自定义的扩展类(一般以Mixin结尾),扩展一个输出的功能
class ListModelMixin():
    def list(self, request):
        tmp = ['1', '2', '3']
        print(tmp)


# 以下代码用于定义类视图
# 我们定义了哪些跟请求方法同名的函数,那么该视图只能接收对应的请求方法
class RegisterView(View, ListModelMixin):
    def get(self, request):
        '''
        处理get逻辑
        :param request: 请求对象
        :return: 响应对象
        '''
        # 测试类视图的扩展类
        self.list(request)
        return http.HttpResponse('假装返回注册页面')

    def post(self, request):
        return http.HttpResponse('假装处理注册逻辑')


class LoginView(View):
    def get(self, request):
        return http.HttpResponse('假装这是个登录页面')

    def post(self, request):
        return http.HttpResponse('假装实现登录逻辑')


class ImageView(View):
    def get(self, request):
        with open('c:/Users/deidei666/Desktop/三次握手、四次挥手.png', 'rb') as f:
            image_data = f.read()
        return http.HttpResponse(content=image_data, content_type='image/png')
